package com.beici.waterghost.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.beici.waterghost.mapper.userMapper;
import com.beici.waterghost.model.idRandom;
import com.beici.waterghost.model.resultes;
import com.beici.waterghost.model.user;
import com.beici.waterghost.service.userService;
import com.beici.waterghost.utils.takeUtils;
import com.beici.waterghost.utils.urlSend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class userServiceImpl implements userService {

    @Autowired
    private userMapper userServer;

    @Override
    public resultes selectUserDetailInfo(Long ID) {
        user user_id = (user) takeUtils.verfitySelect(userServer.selectOne(new QueryWrapper<user>().eq("user_id", ID)));
        return resultes.success("获取成功",user_id);
    }

    @Override
    public resultes updateBack(Integer userId, String backgroundUrl) {
        user user = new user();
        user.setUserId(userId);
        user.setUserBack(backgroundUrl);
        int i = userServer.updateById(user);
        return i>0? resultes.success("切换成功",backgroundUrl):resultes.error("修改失败请联系管理员");
    }
    @Override
    public resultes updateHead(Integer userId, String HeadUrl) {
        user user = new user();
        user.setUserId(userId);
        user.setUserHeader(HeadUrl);
        int i = userServer.updateById(user);
        return i>0? resultes.success("切换成功",HeadUrl):resultes.error("修改失败请联系管理员");
    }
    @Override
    public resultes regUser(String userName,String userPassword,Integer userSex,Long userPhone,String userHeader) {
       userPassword= bCryptPasswordEncoder().encode(userPassword);
        int intRandom = idRandom.getIntRandom();
        while (userServer.selectCount(new QueryWrapper<user>().eq("user_id",intRandom))>0){
            intRandom = idRandom.getIntRandom();
        }
        userServer.adduser(intRandom,userName,userPassword,userSex,userPhone,userHeader);
        urlSend.baiDuSend("http://data.zz.baidu.com/urls?site=https://www.waghost.com&token=PDsOBOpIJZnvppeC"
                ,new String[]{"https://www.waghost.com/personcenter/"+intRandom}
        );
        return resultes.success("注册成功",intRandom);
    }

    @Bean
    private BCryptPasswordEncoder bCryptPasswordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Override
    public resultes phoneIsEntity(Long phone) {
        Integer user_phone = userServer.selectCount(new QueryWrapper<user>().eq("user_phone", phone));

        return user_phone>0?resultes.error("该手机号码已被注册"):resultes.success("",true);
    }

    @Override
    public resultes updatUserMess(Integer userId,String mess) {
        user u = new user();
        u.setUserId(userId);
        u.setUserMess(mess);
        userServer.updateById(u);
        return resultes.success("修改成功",null);
    }
}
